const state = {
  userInfo: null,
  token: null,
  items: []
}

const getters = {
  isAuthenticated: (state) => !!state.token,
  getUserInfo: (state) => state.userInfo,
  items: (state) => state.items
}

const mutations = {
  SET_TOKEN(state, token) { state.token = token },
  SET_USER(state, info) { state.userInfo = info },
  SET_ITEMS(state, items) { state.items = items }
}

const actions = {
  login({ commit }, payload) {
    commit('SET_TOKEN', payload && payload.token)
    commit('SET_USER', payload && payload.userInfo)
  },
  logout({ commit }) {
    commit('SET_TOKEN', null)
    commit('SET_USER', null)
  }
}

export default { namespaced: true, state, getters, mutations, actions }
